package de.lang.rollo.controller.services;

import java.net.UnknownHostException;
import java.util.Date;

import org.quartz.Job;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.Trigger;
import org.quartz.TriggerUtils;
import org.quartz.core.jmx.JobDetailSupport;
import org.quartz.impl.JobDetailImpl;

import com.mongodb.DB;
import com.mongodb.Mongo;
import com.mongodb.MongoClient;
import com.yammer.dropwizard.Service;
import com.yammer.dropwizard.assets.AssetsBundle;
import com.yammer.dropwizard.config.Bootstrap;
import com.yammer.dropwizard.config.Environment;

import de.lang.rollo.controller.configurations.GpioConfiguration;
import de.lang.rollo.controller.enums.EventType;
import de.lang.rollo.controller.logic.GPIOFacade;
import de.lang.rollo.controller.logic.GPIOFactory;
import de.lang.rollo.controller.logic.scheduling.RolloJob;
import de.lang.rollo.controller.managed.GPIOManaged;
import de.lang.rollo.controller.managed.MongoManaged;
import de.lang.rollo.controller.managed.SchedulerManaged;
import de.lang.rollo.controller.model.RolloEvent;
import de.lang.rollo.controller.model.RolloMotor;
import de.lang.rollo.controller.model.Schalter;
import de.lang.rollo.controller.ressources.HtmlPageResource;
import de.lang.rollo.controller.ressources.RolloAccessRessource;

public class RolloControllerService extends Service<GpioConfiguration> {
	public static void main(String[] args) throws Exception {
		new RolloControllerService().run(args);
	}
	
	public RolloControllerService(){
	}

	@Override
	public void initialize(Bootstrap<GpioConfiguration> bootstrap) {
		bootstrap.setName("rollo");
		bootstrap.addBundle(new AssetsBundle("/assets/", "/"));
	}

	@Override
	public void run(GpioConfiguration configuration, Environment environment)
			throws UnknownHostException, SchedulerException {

		// MANAGED
		 MongoManaged mongoManaged = new MongoManaged(configuration);
		 environment.manage(mongoManaged);

		// APP-STUFF
		// GPIOFacade gpioFacade = GPIOFactory.createFacade(configuration,
		// motoren, schalter);
		GPIOManaged gpioManaged = new GPIOManaged(configuration);
		environment.manage(gpioManaged);

		// SCHEDULER		
		SchedulerManaged schedManaged = new SchedulerManaged();
		environment.manage(schedManaged);
		

		// HEALTH CHECKS
		// environment.addHealthCheck(new MongoHealthCheck(mongoClient));

		// RESSOURCES
		environment.addResource(new RolloAccessRessource(GPIOManaged.getFacade()));
		environment.addResource(new HtmlPageResource());
	}
}
